package com.laviedesfons.jhmtasc.types.internals;

import com.laviedesfons.jhmtasc.annotations.binding.MType;
import com.laviedesfons.jhmtasc.types.interfaces.InternalType;
import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.Custom;
import fr.x9c.cadmium.kernel.Value;

/**
 *
 * @author Romano
 */
@MType(camlType="int32")
public final class PInt32 implements InternalType {

    private int value;

    public PInt32() { }
    public PInt32(int v){
        this.value = v;
    }

    @Override
    public InternalType fromValue(Value v) {
        this.value = v.asBlock().asInt32();
        return this;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final PInt32 other = (PInt32) obj;
        if (this.value != other.value) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 11 * hash + this.value;
        return hash;
    }

    @Override
    public Value toValue() {
        Block b = Block.createCustom(Custom.INT_32_SIZE, Custom.INT_32_OPS);
        b.setInt32(value);
        return Value.createFromBlock(b);
    }

    @Override
    public String toString() {
        return String.valueOf(value);
    }
}
